﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace RatingHelper.Controls
{
    //[Designer(typeof(ToolBtnListViewer))]
    public partial class ToolBtnListViewer : UserControl
    {
        public ToolBtnListViewer()
        {
            InitializeComponent();

        }

        /// <summary>
        /// inner list view
        /// </summary>
        public ListView InnerListViewer
        {
            get
            {
                return this.innerListView;
            }
        }


        /// <summary>
        /// list viewer column
        /// </summary>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public ListView.ColumnHeaderCollection InnerColumns
        {
            get
            {
                return this.innerListView.Columns;
            }
        }

        /// <summary>
        /// record toolstrip
        /// </summary>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public ToolStrip RecordToolStrip
        {
            get
            {
                return this.recordToolStrip;
            }
        }



        #region new record button click related
        public delegate void NewBtnClickHandler(object sender, EventArgs e);
        public event NewBtnClickHandler NewBtnClick;
        #endregion


        #region update record button click related
        public delegate void UpdateBtnClickHandler(object sender, EventArgs e);
        public event UpdateBtnClickHandler UpdateBtnClick;
        #endregion


        #region delete record button clicked related
        public delegate void DelBtnClickHandler(object sender, EventArgs e);
        public event DelBtnClickHandler DelBtnClick;
        #endregion

        #region inner list view selected index changed related
        public delegate void InnerListViewSelectedIndexChangedHandler(object sender, EventArgs e);
        public event InnerListViewSelectedIndexChangedHandler InnerListViewSelectedIndexChanged;
        #endregion


        private void NewButton_Click(object sender, EventArgs e)
        {
            this.DisableToolBtns();

            if (this.NewBtnClick != null)
                this.NewBtnClick.Invoke(sender, e);
        }

        /// <summary>
        /// disable all button in tool strip
        /// </summary>
        public void DisableToolBtns()
        {
            this.insertToolStripButton.Enabled = false;
            this.updateToolStripButton.Enabled = false;
            this.delToolStripButton.Enabled = false;
            this.innerListView.Enabled = false;
        }

        /// <summary>
        /// enable all button in tool strip
        /// </summary>
        public void EnableToolBtns()
        {
            this.insertToolStripButton.Enabled = true;
            this.updateToolStripButton.Enabled = true;
            this.delToolStripButton.Enabled = true;
            this.innerListView.Enabled = true;
        }

        private void UpdateButton_Click(object sender, EventArgs e)
        {
            if (this.innerListView.SelectedItems.Count == 0)
            {
                MessageBox.Show("请先选择一条记录！");
                return;
            }

            this.DisableToolBtns();
            if (this.UpdateBtnClick != null)
            {
                this.UpdateBtnClick.Invoke(sender, e);
            }
        }

        private void DelButton_Click(object sender, EventArgs e)
        {
            if (this.innerListView.SelectedItems.Count == 0)
            {
                MessageBox.Show("请先选择一条记录！");
                return;
            }
            if (this.DelBtnClick != null)
            {
                this.DelBtnClick.Invoke(sender, e);
            }
        }



        private void InnerListView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.innerListView.SelectedItems.Count == 0)
            {
                return;
            }

            if (this.InnerListViewSelectedIndexChanged != null)
            {
                this.InnerListViewSelectedIndexChanged.Invoke(sender, e);

            }
        }

    }
}
